// pages/orderin/orderin.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modalActive:false,
    domain:app.globalData.baseUrl,
    date:'',
    tdate:'',
    alldays:'',
    homestay:'',
    arriveTime: ['14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '24:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00'],
    index:0,
    homestay_dates:[],
    sum:'',
    saveSub:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    let d1 = new Date(options.date.replace(/-/g, "/"));
    let d2 = new Date(options.tdate.replace(/-/g, "/"));
    let times = d2.getTime() - d1.getTime();
    let alldays = parseInt(times / (1000 * 60 * 60 * 24));
    this.setData({
      date:options.date,
      tdate:options.tdate,
      alldays:alldays,
      homestay:JSON.parse(options.homestay)
    })
    wx.request({
      url: app.globalData.baseUrl + '/api/index/homestay_dates',
      data: {
        id:that.data.homestay.id,
        date:that.data.date,
        tdate:that.data.tdate
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          homestay_dates:res.data.data,
          sum:res.data.sum
        })
      }
    });
    wx.request({
      url: app.globalData.baseUrl + '/api/index/systeminfos',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        res.data.data.tip= res.data.data.tip.replace(/&amp;/g,'&');
        res.data.data.tip= res.data.data.tip.replace(/&nbsp;/g,'\xa0');
        res.data.data.tip= res.data.data.tip.replace(/&quot;/g,'"');
        that.setData({
          systeminfos:res.data.data
        })
      }
    });
  },
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value
    })
  },
  billModal:function(e){
    var that=this
    this.setData({
      modalActive:true
    })
  }, 
  modalDis:function(){
    var that=this
    this.setData({
      modalActive:false
    })
  },
  formSubmit:function(e){
    var that=this
    if(!(/^[\u4e00-\u9fa5]{2,4}$/.test(e.detail.value.name))){ 
      wx.showToast({
        title: '姓名有误',
        icon:'none'
      })
      return false; 
    }
    if(!(/^1[3456789]\d{9}$/.test(e.detail.value.phone))){ 
      wx.showToast({
        title: '手机号有误',
        icon:'none'
      })
      return false; 
    }
    that.setData({
      saveSub:true
    })
    var hd_idArr=[];
    for (var i = 0; i < that.data.homestay_dates.length; i++) {
      hd_idArr.push(that.data.homestay_dates[i].id)
    }
    wx.request({
      url: app.globalData.baseUrl + '/api/order/orderIn', //支付接口
      data: {
        openid: app.globalData.openid,
        homestay_title:that.data.homestay.des,
        date:that.data.date,
        tdate:that.data.tdate,
        alldays:that.data.alldays,
        h_id:that.data.homestay.id,
        hd_id:hd_idArr.toString(),
        payfee: that.data.payfee,
        name:e.detail.value.name,
        phone:e.detail.value.phone,
        rooms:e.detail.value.rooms,
        arrive_time:that.data.arriveTime[that.data.index],
        payfee:that.data.sum
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(tmdres) {
        if(tmdres.data.code==200){
          wx.requestPayment({
            timeStamp: tmdres.data.data.timeStamp,
            nonceStr: tmdres.data.data.nonceStr,
            package: tmdres.data.data.package,
            signType: tmdres.data.data.signType,
            paySign: tmdres.data.data.paySign,
            success: function (respay) {
              console.log(respay)
              wx.showToast({
                title: '付款成功',
                icon:'none'
              })
            },
            fail(reserr) {
              wx.showToast({
                title: '付款失败',
                icon:'none'
              })
            }
          })
        }else{
          wx.showToast({
            title: tmdres.data.msg,
            icon:'none'
          })
        }
        
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})